var title = [
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x07,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xE0,0x00,
  0x00,0x08,0x00,0x60,0x00,0x00,0x00,0x1F,0x00,0x00,0x00,0x03,0xF4,0x10,0x00,
  0x00,0x0B,0xFF,0x10,0x00,0x00,0x00,0x20,0x80,0x00,0x00,0x04,0x09,0xD0,0x00,
  0x00,0x0B,0x83,0xD0,0x00,0x00,0x00,0x2E,0xB8,0x00,0x00,0x05,0xE1,0xD0,0x00,
  0x00,0x0B,0x83,0xD0,0x00,0x00,0x00,0x2E,0x44,0x00,0x00,0x05,0xE1,0xD0,0x00,
  0x00,0x0B,0x83,0xD0,0x00,0x00,0x00,0x2E,0x34,0x00,0x00,0x05,0xE1,0xD0,0x00,
  0x00,0x0B,0x83,0xCF,0xFF,0xF9,0xFF,0xAE,0x35,0xFF,0x7C,0xF9,0xE1,0xD0,0x00,
  0x00,0x0B,0x83,0xC0,0x00,0x06,0x00,0x4E,0x66,0x00,0x83,0x01,0xE1,0xD0,0x00,
  0x00,0x0B,0xFF,0x13,0xE7,0xF0,0xFF,0x0F,0xE0,0x7E,0x38,0x73,0xF9,0xD0,0x00,
  0x00,0x0B,0xFF,0x13,0xE7,0xF0,0xFF,0x0F,0xC0,0x7E,0x38,0x73,0xF9,0xD0,0x00,
  0x00,0x0B,0x83,0xDC,0x0E,0x0C,0x01,0xCF,0x80,0xE1,0x38,0x71,0xE1,0xD0,0x00,
  0x00,0x0B,0x83,0xDC,0xCE,0x0C,0x01,0xCF,0xC0,0xE1,0x38,0x71,0xE1,0xD0,0x00,
  0x00,0x0B,0x83,0xDD,0x2F,0xF0,0x7F,0xCE,0xE0,0xE1,0x38,0x71,0xE0,0x10,0x00,
  0x00,0x0B,0x83,0xDD,0x2F,0xF0,0xFF,0xCE,0x70,0xE1,0x38,0x71,0xE0,0x10,0x00,
  0x00,0x0B,0x83,0xDD,0x2E,0x00,0x81,0xCE,0x38,0xE1,0x38,0x71,0xE1,0xD0,0x00,
  0x00,0x0B,0xFF,0x1D,0x27,0xFC,0xFE,0xCE,0x1C,0x7E,0x1F,0xC4,0x79,0xD0,0x00,
  0x00,0x0B,0xFF,0x1D,0x17,0xFC,0x7E,0xCE,0x0C,0x7E,0x1F,0xCA,0x79,0xD0,0x00,
  0x00,0x08,0x00,0x41,0x10,0x01,0x00,0x00,0xE1,0x00,0xC0,0x11,0x00,0x10,0x00,
  0x00,0x07,0xFF,0xBE,0x0F,0xFE,0xFF,0xFF,0x1E,0xFF,0x3F,0xE0,0xFF,0xE0,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x90,0x04,0x04,0x10,0x03,0x8A,0x10,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x90,0x04,0x00,0x00,0x02,0x08,0x80,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0xF3,0x35,0x54,0xD7,0x63,0x1A,0xD7,0x60,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x95,0x46,0x54,0x95,0x52,0x2A,0x95,0x50,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x93,0x35,0x25,0x97,0x53,0x9A,0x57,0x50,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0xF0,0x06,0x04,0x00,0x04,0x00,0x38,0x00,0x0A,0x00,0x00,0x00,
  0x00,0x00,0x00,0x88,0x09,0x04,0x00,0x20,0x00,0x44,0x00,0x02,0x00,0x00,0x00,
  0x00,0x00,0x00,0xF2,0x84,0x27,0x31,0xB5,0x98,0x40,0xC6,0x6A,0x80,0x00,0x00,
  0x00,0x00,0x00,0x8A,0x82,0x54,0x8A,0x24,0x54,0x4D,0x28,0x8B,0x00,0x00,0x00,
  0x00,0x00,0x00,0x8A,0x89,0x44,0xA8,0xA5,0x54,0x45,0x22,0x8A,0x80,0x00,0x00,
  0x00,0x00,0x00,0xF1,0x06,0x37,0x1B,0x14,0xD4,0x3C,0xCC,0x6A,0x80,0x00,0x00,
  0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
];

var fire = [
  57,8,
  0xF8,0x00,0x00,0x00,0x3D,0xEF,0x8F,0x80,
  0xCC,0x00,0x00,0x00,0x60,0xCC,0xD8,0x00,
  0xCC,0x00,0x00,0x00,0x60,0xCC,0xD8,0x00,
  0xF9,0x67,0x1E,0x78,0x78,0xCF,0x9F,0x00,
  0xC1,0x8C,0xA0,0x80,0x60,0xCC,0xD8,0x00,
  0xC1,0x8F,0x1C,0x70,0x60,0xCC,0xD8,0x00,
  0xC1,0x8C,0x02,0x08,0x60,0xCC,0xDF,0x80,
  0xC1,0x87,0xBC,0xF0,0x61,0xEC,0xCF,0x80
];

var arrow = [
  5,5,
  0x20,
  0x10,
  0xF8,
  0x10,
  0x20
];

var OLED_DC = 8;
var OLED_CS = 10;   // SPI slave-select
var OLED_CLK = 13;  // hardware SPI clock
var OLED_MOSI = 11;   // hardware SPI MOSI
var OLED_RESET = 7;

var display = new Arduboy();

var width = 128;   //Width of screen
var height = 64;   //Hight of screen
var dx = -1;        //Initial movement of ball
var dy = -1;        //Initial movement of ball
var xb;           //Balls starting possition
var yb;           //Balls starting possition
var released;     //If the ball has been released by the player
var paused = false;   //If the game has been paused
var xPaddle;       //X position of paddle

var isHit = mArray(5, 12); //[5][12];   //Array of if bricks are hit or not

var bounced = false;  //Used to fix double bounce glitch
var lives = 3;       //Amount of lives
var level = 1;       //Current level
var score=0;   //Score for the game
var brickCount;  //Amount of bricks hit
var pad,pad2,pad3;     //Button press buffer used to stop pause repeating
var oldpad,oldpad2,oldpad3;
var text = mArray(16); //[16];      //General string buffer
var start=false;    //If in menu or in game
var initialDraw=false;//If the inital draw has happened
var initials = mArray(3);     //Initials used in high score

//Ball Bounds used in collision detection
var leftBall;
var rightBall;
var topBall;
var bottomBall;

//Brick Bounds used in collision detection
var leftBrick;
var rightBrick;
var topBrick;
var bottomBrick;

var ballclock = 0;

var arduino = [
  0x3F, 0xFF, 0xFF, 0xFC, 0x40, 0x00, 0x00, 0x02, 0x89, 0x99,0x54,
  0x91, 0x95, 0x55, 0x56, 0xA9, 0x9D, 0x95, 0x55, 0xA9, 0x95, 0x59,
  0xD4, 0x91, 0x40, 0x00, 0x00, 0x02, 0x3F, 0xFF, 0xFF, 0xFC
];

function intro() {
  for(var i = -8; i < 28; i = i + 2) {
    display.clearDisplay();
    display.setCursor(46, i);
    display.print("ARDUBOY");
    display.display();
  }

  tone(A2, 987, 160);
  delay(160);
  tone(A2, 1318, 400);
  delay(2000);
}

function setup() {
  SPI.begin();
  display.start();
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print("Hello World!");
  display.display();
  intro();
}

function movePaddle() {
  //Move right
  if(xPaddle < width - 12) {
    if( !digitalRead(5) ) {
      xPaddle++;
    }
  }

  //Move left
  if(xPaddle > 0) {
    if( !digitalRead(9)) {
      xPaddle--;
    }
  } 
}

function moveBall() {
  if(released) {
    //Move ball
    xb=xb + dx;
    yb=yb + dy;

    //Set bounds
    leftBall = xb;
    rightBall = xb + 2;
    topBall = yb;
    bottomBall = yb + 2;

    //Bounce off top edge
    if (yb <= 0) {
      yb = 2;
      dy = -dy;
      tone(A2, 523, 250);
    }

    //Lose a life if bottom edge hit
    if (yb >= 64) {
      display.drawRect(xPaddle, 63, 11, 1, 0);
      xPaddle = 54;
      yb=60; 
      released = false;
      lives--;
      drawLives();
      tone(A2, 175, 250);
      if (random(0, 2) == 0) {
        dx = 1;
      } else {
        dx = -1;
      }
    }

    //Bounce off left side
    if (xb <= 0) {
      xb = 2;
      dx = -dx;
      tone(A2, 523, 250);
    }

    //Bounce off right side
    if (xb >= width - 2) {
      xb = width - 4;
      dx = -dx;
      tone(A2, 523, 250);
    }

    //Bounce off paddle
    if (xb+1>=xPaddle && xb<=xPaddle+12 && yb+2>=63 && yb<=64) {
      dy = -dy;
      dx = ((xb-(xPaddle+6))/3); //Applies spin on the ball
      tone(A2, 200, 250);
    }

    //Bounce off Bricks
    for (var row = 0; row < 4; row++) {
      for (var column = 0; column < 14; column++) {
        if (!isHit[row][column]) {
          //Sets Brick bounds
          leftBrick = 10 * column;
          rightBrick = 10 * column + 10;
          topBrick = 6 * row + 1;
          bottomBrick = 6 * row + 7;

          //If A collison has occured
          if (topBall <= bottomBrick && bottomBall >= topBrick && leftBall <= rightBrick && rightBall >= leftBrick) {
            printScore();
            brickCount++;
            isHit[row][column] = true;
            display.drawRect(10*column, 2+6*row, 8, 4, 0);

            //Vertical collision
            if (bottomBall > bottomBrick || topBall < topBrick) {
              //Only bounce once each ball move
              if(!bounced) {
                dy =- dy;
                yb += dy;
                bounced = true;
                tone(A2, 261, 250);
              } 
            }

            //Hoizontal collision
            if (leftBall < leftBrick || rightBall > rightBrick) {
              //Only bounce once brick each ball move
              if(!bounced) {
                dx =- dx;
                xb += dx;
                bounced = true;
                tone(A2, 261, 250);
              } 
            }
          }
        }
      }
    }
    //Reset Bounce
    bounced = false;
  } else {
    //Ball follows paddle
    xb=xPaddle + 5;
    
    //Release ball if FIRE pressed
    pad3 = !digitalRead(A0);
    if (pad3 == 1 && oldpad3 == 0) {  
      released=true;

      //Apply random direction to ball on release
      if (random(0, 2) == 0) {
        dx = 1;
      } else {
        dx = -1;
      }
      
      //Makes sure the ball heads upwards 
      dy = -1;
    }
    
    oldpad3 = pad3; 
  }
}

function drawBall() {
  display.drawPixel(xb,   yb,   0);
  display.drawPixel(xb+1, yb,   0);
  display.drawPixel(xb,   yb+1, 0);
  display.drawPixel(xb+1, yb+1, 0);

  if(ballclock>4) {
    moveBall();
    ballclock=0;
  }

  ballclock++;

  display.drawPixel(xb,   yb,   1);
  display.drawPixel(xb+1, yb,   1);
  display.drawPixel(xb,   yb+1, 1);
  display.drawPixel(xb+1, yb+1, 1);
}

function drawPaddle()
{
  display.drawRect(xPaddle, 63, 11, 1, 0);
  movePaddle();
  display.drawRect(xPaddle, 63, 11, 1, 1);
}

function drawLives()
{
  var text = sprintf("LIVES:%u", lives);
  display.setCursor(0, 90);
  display.print(text);
}

function drawGameOver()
{
  display.drawPixel(xb,   yb,   0);
  display.drawPixel(xb+1, yb,   0);
  display.drawPixel(xb,   yb+1, 0);
  display.drawPixel(xb+1, yb+1, 0);
  display.setCursor(52, 42);
  display.print( "Game");
  display.setCursor(52, 54);
  display.print("Over");
  display.display();
  delay(4000);
}

function pause() {
  paused = true;
  
  //Draw pause to the screen
  display.setCursor(52, 45);
  display.print("PAUSE");
  display.display();
  
  while (paused) {
    delay(150);
    
    //Unpause if FIRE is pressed
    pad2 = !digitalRead(A0);
    if (pad2 > 1 && oldpad2 == 0 && released) {
        display.fillRect(52, 45, 30, 11, 0);
        
        paused=false;
    }
    
    oldpad2=pad2; 
  }
}

function printScore() {
  score += (level*10);
  var text = sprintf("SCORE:%u", score);
  display.setCursor(80, 90);
  display.print(text); 
}

function newLevel() {
  //Undraw paddle
  display.drawRect(xPaddle, 63, 11, 1, 0);

  //Undraw ball
  display.drawPixel(xb,   yb,   0);
  display.drawPixel(xb+1, yb,   0);
  display.drawPixel(xb,   yb+1, 0);
  display.drawPixel(xb+1, yb+1, 0);

  //Alter various variables to reset the game
  xPaddle = 54;
  yb = 60;
  brickCount = 0;
  released = false;

  //Draws new bricks and resets their values
  for (var row = 0; row < 4; row++) {
    for (var column = 0; column < 13; column++) {
        isHit[row][column] = false;
        display.drawRect(10*column, 2+6*row, 8, 4, 1);
    }
  }
  
  //Draws the initial lives
  drawLives();
  
  //Draws the initial score
  var text = sprintf("SCORE:%u", score);
  display.setCursor(80, 90);
  display.print(text); 
}

//Used to delay images while reading button input
function pollFireButton(n) {
  for(var i = 0; i < n; i++) {
    delay(15);
    pad = !digitalRead(A0);
    if(pad == 1 && oldpad == 0) {
      oldpad3 = 1; //Forces pad loop 3 to run once
      return true;
    }
    
    oldpad = pad;
  }
  
  return false;
}

//Function by nootropic design to display highscores
function displayHighScores(file) {
  var y = 10;
  var x = 24;
  
  // Each block of EEPROM has 10 high scores, and each high score entry
  // is 5 bytes long:  3 bytes for initials and two bytes for score.
  var address = file*10*5;
  var hi, lo;
  display.clearDisplay();
  display.setCursor(32, 0);
  display.print("HIGH SCORES");
  display.display();

  for(var i = 0; i < 10; i++) {
    var text = sprintf("%2d", i+1);
    display.setCursor(x,y+(i*8));
    display.print( text);
    display.display();
    hi = EEPROM.read(address + (5*i));
    lo = EEPROM.read(address + (5*i) + 1);

    if ((hi == 0xFF) && (lo == 0xFF)) {
      score = 0;
    } else {
      score = (hi << 8) | lo;
    }

    initials[0] = EEPROM.read(address + (5*i) + 2);
    initials[1] = EEPROM.read(address + (5*i) + 3);
    initials[2] = EEPROM.read(address + (5*i) + 4);

    if (score > 0) {
      var text = sprintf("%c%c%c %u", initials[0], initials[1], initials[2], score);
      display.setCursor(x + 24, y + (i*8));
      display.print(text);
      display.display();
    }
  }
  
  if (pollFireButton(300)) {
    return true;
  }
  
  return false;
}

function titleScreen() {
    console.log("title");
    
  //Clears the screen
  display.clearDisplay();
  display.setCursor(16,22);
  display.setTextSize(2);
  display.print("ARAKNOID");
  display.setTextSize(1);
  display.display();
  
  if (pollFireButton(25)) {
    return true;
  }

  //Flash "Press FIRE" 5 times
  for(var i = 0; i < 5; i++) {
    //Draws "Press FIRE"
    //display.bitmap(31, 53, fire);  display.display();
    display.setCursor(31, 53);
    display.print("PRESS FIRE!");
    display.display();

    if (pollFireButton(50)) {
      return true;
    }
    
    //Removes "Press FIRE"
    display.clearDisplay();
    display.setCursor(16,22);
    display.setTextSize(2);
    display.print("ARAKNOID");
    display.setTextSize(1);
    display.display();

    display.display();
    if (pollFireButton(25)) {
      return true;
    }
  }

  return false;
}

//Function by nootropic design to add high scores
function enterInitials() {
  var index = 0;

  display.clearDisplay();

  initials[0] = ' ';
  initials[1] = ' ';
  initials[2] = ' ';

  while (true) {
    display.display();
    display.clearDisplay();

    display.setCursor(16,0);
    display.print("HIGH SCORE");
    var text = sprintf("%u", score);
    display.setCursor(88, 0);
    display.print(text);
    display.setCursor(56, 20);
    display.print(initials[0]);
    display.setCursor(64, 20);
    display.print(initials[1]);
    display.setCursor(72, 20);
    display.print(initials[2]);
    
    for(var i = 0; i < 3; i++) {
      display.drawLine(56 + (i*8), 27, 56 + (i*8) + 6, 27, 1);
    }
    
    display.drawLine(56, 28, 88, 28, 0);
    display.drawLine(56 + (index*8), 28, 56 + (index*8) + 6, 28, 1);
    delay(150);

    if (!digitalRead(5)) {
      index--;
      if (index < 0) {
        index = 0;
      } else {
        tone(A2, 1046, 250);
      }
    }

    if (!digitalRead(9)) {
      index++;
      if (index > 2) {
        index = 2;
      }  else {
        tone(A2, 1046, 250);
      }
    }

    if (!digitalRead(8)) {
      initials[index]++;
      tone(A2, 523, 250);
      
      // A-Z 0-9 :-? !-/ ' '
      if (initials[index] == '0') {
        initials[index] = ' ';
      }
      if (initials[index] == '!') {
        initials[index] = 'A';
      }
      if (initials[index] == '[') {
        initials[index] = '0';
      }
      if (initials[index] == '@') {
        initials[index] = '!';
      }
    }

    if (!digitalRead(10)) {
      initials[index]--;
      tone(A2, 523, 250);
      
      if (initials[index] == ' ') {
        initials[index] = '?';
      }
      if (initials[index] == '/') {
        initials[index] = 'Z';
      }
      if (initials[index] == 31) {
        initials[index] = '/';
      }
      if (initials[index] == '@') {
        initials[index] = ' ';
      }
    }

    if (!digitalRead(A0)) {
      if (index < 2) {
        index++;
        tone(A2, 1046, 250);
      } else {
        tone(A2, 1046, 250);
        return;
      }
    }
  }
}

function enterHighprintScore(file) {
  // Each block of EEPROM has 10 high scores, and each high score entry
  // is 5 bytes long:  3 bytes for initials and two bytes for score.
  var address = file * 10 * 5;
  var hi, lo;
  var tmpInitials = []; //[3];
  var tmpScore = 0;

  // High score processing
  for(var i = 0; i < 10; i++) {
    hi = EEPROM.read(address + (5*i));
    lo = EEPROM.read(address + (5*i) + 1);
    
    if ((hi == 0xFF) && (lo == 0xFF)) {
      // The values are uninitialized, so treat this entry 
      // as a score of 0.
      tmpScore = 0;
    } else {
      tmpScore = (hi << 8) | lo;
    }
    
    if (score > tmpScore) {
      enterInitials();
      
      for(var j=i;j<10;j++) {
        hi = EEPROM.read(address + (5*j));
        lo = EEPROM.read(address + (5*j) + 1);

        if ((hi == 0xFF) && (lo == 0xFF)) {
            tmpScore = 0;
        } else {
          tmpScore = (hi << 8) | lo;
        }

        tmpInitials[0] = EEPROM.read(address + (5*j) + 2);
        tmpInitials[1] = EEPROM.read(address + (5*j) + 3);
        tmpInitials[2] = EEPROM.read(address + (5*j) + 4);

        // write score and initials to current slot
        EEPROM.write(address + (5*j), ((score >> 8) & 0xFF));
        EEPROM.write(address + (5*j) + 1, (score & 0xFF));
        EEPROM.write(address + (5*j) + 2, initials[0]);
        EEPROM.write(address + (5*j) + 3, initials[1]);
        EEPROM.write(address + (5*j) + 4, initials[2]);

        // tmpScore and tmpInitials now hold what we want to
        //write in the next slot.
        score = tmpScore;
        initials[0] = tmpInitials[0];
        initials[1] = tmpInitials[1];
        initials[2] = tmpInitials[2];
      }

      score = 0;
      initials[0] = ' ';
      initials[1] = ' ';
      initials[2] = ' ';

      return;
    }
  }
}

function loop() {
  display.display();

  //Title screen loop switches from title screen
  //and high scores utill FIRE is pressed
  /*if (!start) {
    start = titleScreen();
    if (!start) {
      start = displayHighScores(2);
    }
    
    return;
  }*/

  //Inital level draw
  if (!initialDraw) {
    //Clears the screen 
    display.display();
    display.clearDisplay();
    //Selects Font
    //Draws the new level
    newLevel();
    initialDraw=true;
  }

  if (lives>0) {
    drawPaddle();

    //Pause game if FIRE pressed
    pad = !digitalRead(A0);

    if(pad >1 && oldpad==0 && released) {
      oldpad2=0; //Forces pad loop 2 to run once
      pause();
    }

    oldpad=pad;
    drawBall();

    if(brickCount==60) {
      level++;
      newLevel(); 
    }
  } else {
    drawGameOver();
    if (score > 0) {
      enterHighprintScore(2);
    }

    display.clearDisplay();
    initialDraw=false;
    start=false;
    lives=3;
    score=0;
    newLevel();
  }
}
